//
//  GameAllViewController.swift
//  StudyDY
//
//  Created by yinchangming on 2018/7/9.
//  Copyright © 2018年 yinchangming. All rights reserved.
//

import UIKit


//item的宽度
let kItemW=(kScreenW - kCollctionMarging*2) / 3
//item的高度
let kItemH = kItemW * 5 / 6
let kGameCellID="kGameCellID"
let kGameHeadID="kGameHeadID"
let kUseForH:CGFloat=40

class GameAllViewController: BaseViewController {
    private lazy var gameVM:GameViewModel=GameViewModel()
    
    //懒加载 顶部常用游戏(标题)
    lazy var useforView={ () -> CollectionHeaderView in
        let useFor=CollectionHeaderView.creat()
        useFor.frame=CGRect(x: 0, y: -(kUseForH+kGameH), width: kScreenW, height: kUseForH)
        useFor.groupImage.image=UIImage(named: "Img_orange")
        useFor.groupTitleLabel.text="常见"
        useFor.moreBtn.isHidden=true
        return useFor
    }()
       //懒加载 顶部常用游戏(游戏栏目)
    lazy var gameView={() -> GameView in
        let gameView=GameView.creatView()
        gameView.frame=CGRect(x: 0, y: -kGameH, width: kScreenW, height: kGameH)
        return gameView
    }()
    
    lazy var collection={ () ->  UICollectionView in
        //布局
        let layout=UICollectionViewFlowLayout()
        layout.itemSize=CGSize(width: kItemW, height: kItemH)
        layout.minimumLineSpacing=0
        layout.minimumInteritemSpacing=0
        layout.sectionInset=UIEdgeInsets(top: 0, left: kCollctionMarging, bottom: 0, right: kCollctionMarging)
        // 设置头部的大小
        layout.headerReferenceSize=CGSize(width: kScreenW, height: CGFloat( kPageTitleH))
        
        let collectionView=UICollectionView(frame: self.view.bounds, collectionViewLayout: layout)
        collectionView.autoresizingMask=[.flexibleWidth , .flexibleHeight]
        collectionView.backgroundColor=UIColor.white
        collectionView.register(UINib(nibName: "GameCollectionCell", bundle: nil), forCellWithReuseIdentifier: kGameCellID)
        collectionView.register(UINib(nibName: "CollectionHeaderView", bundle: nil), forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: kGameHeadID)
        collectionView.contentInset=UIEdgeInsets(top: kUseForH+kGameH, left: 0, bottom: 0, right: 0 )
        collectionView.dataSource=self
        return collectionView
    }()
    override func viewDidLoad() {
        contentView=collection
        updateUI()
            super.viewDidLoad()
    }
}
//更新
extension GameAllViewController{
    func updateUI(){
        self.view.addSubview(collection)
        collection.addSubview(useforView)
        collection.addSubview(gameView)
        getDatasFromNet()
    }
    func getDatasFromNet() {
        gameVM.requsetGame {
            self.collection.reloadData()
            //  取所有 游戏的前是个左右常见游戏数据
            var anchor:[AnchorCatgary]=[AnchorCatgary]()
            for (index , item)  in  self.gameVM.games.enumerated(){
                if(index>10){
                     break
                }
                let anchorItem=AnchorCatgary()
                anchorItem.tag_name=item.tag_name
                anchorItem.small_icon_url=item.small_icon_url
                anchor.append(anchorItem)
            }
            self.gameView.isFromFrist=false
            self.gameView.anchor=anchor
            self.finshRequst()
        }
    }
}
//实现数据源回调
extension GameAllViewController :UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return gameVM.games.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell=collectionView.dequeueReusableCell(withReuseIdentifier: kGameCellID, for: indexPath) as! GameCollectionCell
        cell.catgary = gameVM.games[indexPath.item]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let header=collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: kGameHeadID, for: indexPath) as! CollectionHeaderView
        header.groupTitleLabel.text="全部"
        header.groupImage.image=UIImage(named: "Img_orange")
        header.moreBtn.isHidden=true
        return header
    }
}




